package com.monkeyboy.util;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

/**
 * 未认证的方式
 * @Author Gavin
 * @date 2021.01.10 16:54
 */
public class MongoDBUtil {
    private static MongoClient client = null;

    static {
        if (client == null) {
            //创建连接对象
            client = new MongoClient("192.168.1.55", 27017);
        }
    }

    //获取mongodb数据库
    public static MongoDatabase getDatabase(String dbName) {
        return client.getDatabase("test");
    }

    //获取mongodb中的集合
    public static MongoCollection<Document> getCollection(String dbname, String collName) {
        MongoDatabase database = getDatabase(dbname);
        return database.getCollection(collName);
    }

    public static void main(String[] args) {
        getCollection("test","test");
    }
}
